// 经理角色功能实现
#include <windows.h>
#include <stdio.h>
#include "../util/LinkedList.h"
#include "../util/XTime.h"
#include "../manage/StaffManage.h"
#include "../manage/DishOrderManage.h"
#include "RoleOfManager.h"

// 输出了经理功能界面   以及相关功能函数
int UiOfManager()
{
	int chooseItem;
	while (1)
	{
		system("cls");
		printf("\n\n\t\t*************** 经理功能界面 ***************\n\n");
		printf("\t\t\t\t[1] 营收汇总\n\n");
		printf("\t\t\t\t[2] 注销\n\n");
		printf("\t\t\t\t[3] 退出");
		printf("\n\n\t\t==> 请输入要查询的条件:");
		chooseItem = FormatInputString(NULL, '1', '3', 1, 2);
		if (chooseItem == 1)
		{
			ComputeTurnover();
		}
		else if (chooseItem == 2)
		{
			return 0;
		}
		else if (chooseItem == 3)
		{
			return 1;
		}
	}
}

// 计算营业额
void ComputeTurnover()
{
	long money = 0;
	long income = 0; // 总金额
	long *startDate, *endDate;
	long ordernum; // 订单号
	ListPointer orderPointer;
	ListPointer staffPointer;
	ListPointer tempPointer1;
	ListPointer tempPointer2;
	struct DishOrder *orderData;
	struct Staff *staffData;
	FILE *filePointer;

	startDate = (long *)malloc(sizeof(long)); // 开始日期
	endDate = (long *)malloc(sizeof(long));	  // 结束日期
	filePointer = FileOpen("dishOrders.txt");
	orderPointer = FileRead(filePointer, sizeof(struct DishOrder));
	filePointer = FileOpen("taffInfos.txt");
	staffPointer = FileRead(filePointer, sizeof(struct Staff));

	system("cls");
	printf("\n\n\t\t*************** 营收额-查询条件选项 ***************\n\n");
	printf("\t\t\t\t[1] 当天查询\n\n");
	printf("\t\t\t\t[2] 历史查询");
	printf("\n\n\t\t==> 请输入要查询的条件:");
	if (FormatInputString(NULL, '1', '2', 1, 2) == 1)
	{
		system("cls");
		// FIXME OrderNoGenerate
		// *startDate = OrderNoGenerate(NULL, NULL); // 获取当前系统日期
		NowTimestamp(startDate); // 获取当前系统日期
		*endDate = *startDate;
	}
	else
	{
		// FIXME OrderDateInputFormat
		OrderDateInputFormat(startDate, endDate); // 输入开始和结束日期
		printf("\n");
	}

	printf("\n\n\t\t*************** 营收查询 ***************\n\n");
	printf("\t\t\t   %-15s%s\n\n", "服务员", "营收");
	tempPointer1 = staffPointer->listNext;
	while (tempPointer1)
	{
		staffData = (struct Staff *)(tempPointer1->listData);
		tempPointer2 = orderPointer->listNext;
		while (tempPointer2)
		{
			orderData = (struct DishOrder *)(tempPointer2->listData);
			sscanf(orderData->orderNo, "%8ld", &ordernum); // 获取订单号上的日期
			if ((strcmp(staffData->staffName, orderData->orderStaff) == 0) &&
				(ordernum <= *endDate) && (ordernum >= *startDate))
			{
				money = money + (orderData->orderTotalMoney);
			}
			tempPointer2 = tempPointer2->listNext;
		}
		if ((staffData->StaffRole == 0)) // 判断是否为服务员
		{
			printf("\t\t\t   %-15s%s%ld\n", staffData->staffName, "￥", money);
			income = income + money; // 累加每个服务员的营业额
			money = 0;
		}
		tempPointer1 = tempPointer1->listNext;
	}

	printf("\n\t\t==> 总营业额：￥%ld\n", income);
	printf("\n\t\t!!! 请按任意键返回...");
	getch();
}
